home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 301-325 / disk_315 / surf / writeilbm.c < prev   
C/C++ Source or Header  |  1992-05-06  |  6KB  |  202 lines

  1. #include <stdio.h>
  2. #include <exec/types.h>
  3. #include <intuition/intuition.h>
  4. #include <graphics/gfxmacros.h>
  5. #ifdef MANX
  6. #include <functions.h>
  7. #endif
  8. #include "mytypes.h"
  9.  
  10. extern int PackRow();
  11. /*
  12.  * following definitions cut from ilbm.h file
  13.  */
  14. typedef UBYTE Masking;          /* Choice of masking technique.*/
  15. #define mskNone                 0
  16. #define mskHasMask              1
  17. #define mskHasTransparentColor  2
  18. #define mskLasso                3
  19.  
  20. typedef UBYTE Compression;      /* Choice of compression algorithm applied to
  21.      * each row of the source and mask planes. "cmpByteRun1" is the byte run
  22.      * encoding generated by Mac's PackBits. See Packer.h . */
  23. #define cmpNone      0
  24. #define cmpByteRun1  1
  25.  
  26. /* Aspect ratios: The proper fraction xAspect/yAspect represents the pixel
  27.  * aspect ratio pixel_width/pixel_height.
  28.  *
  29.  * For the 4 Amiga display modes:
  30.  *   320 x 200: 10/11  (these pixels are taller than they are wide)
  31.  *   320 x 400: 20/11
  32.  *   640 x 200:  5/11
  33.  *   640 x 400: 10/11           */
  34. #define x320x200Aspect 10
  35. #define y320x200Aspect 11
  36. #define x320x400Aspect 20
  37. #define y320x400Aspect 11
  38. #define x640x200Aspect  5
  39. #define y640x200Aspect 11
  40. #define x640x400Aspect 10
  41. #define y640x400Aspect 11
  42.  
  43. /* A BitMapHeader is stored in a BMHD chunk. */
  44. typedef struct {
  45.     UWORD w, h;                 /* raster width & height in pixels */
  46.     WORD  x, y;                 /* position for this image */
  47.     UBYTE nPlanes;              /* # source bitplanes */
  48.     Masking masking;            /* masking technique */
  49.     Compression compression;    /* compression algoithm */
  50.     UBYTE pad1;                 /* UNUSED.  For consistency, put 0 here.*/
  51.     UWORD transparentColor;     /* transparent "color number" */
  52.     UBYTE xAspect, yAspect;     /* aspect ratio, a rational number x/y */
  53.     WORD  pageWidth, pageHeight;  /* source "page" size in pixels */
  54.     } BitMapHeader;
  55.  
  56. /* RowBytes computes the number of bytes in a row, from the width in pixels.*/
  57. #define RowBytes(w)   (((w) + 15) >> 4 << 1)
  58.  
  59.  
  60.  
  61. #define IDSIZE 4
  62.  
  63. void WriteIlbm( filename, win, scrn,packflag )
  64.     char *filename;
  65.     struct NewWindow *win;
  66.     struct NewScreen *scrn;
  67.     bool packflag;
  68. {
  69.     FILE *ofile;
  70.     long formpos; /* position of length following 'FORM' */
  71.     long formsize;
  72.     struct ViewPort *vp;
  73.  
  74.     ofile = fopen(filename,"w");
  75.     if( !ofile ) {
  76.         return;
  77.     }
  78.  
  79.     fwrite("FORM", IDSIZE, 1, ofile);
  80.     formpos = ftell( ofile );
  81.     fwrite( (char *)&formsize, sizeof(formsize), 1, ofile); /* will be rewritten */
  82.     fwrite( "ILBMBMHD", IDSIZE*2, 1, ofile );
  83.  
  84.     {
  85.         BitMapHeader bmhdr;
  86.         long bmhdrsize;
  87.         static UBYTE xaspect[2][2]= { { x320x200Aspect, x320x400Aspect },
  88.                                       { x640x200Aspect, x640x400Aspect }};
  89.         static UBYTE yaspect[2][2]= { { y320x200Aspect, y320x400Aspect },
  90.                                       { y640x200Aspect, y640x400Aspect }};
  91.         int wx, wy;
  92.  
  93.         bmhdrsize = 20;
  94.         fwrite( (char *)&bmhdrsize, sizeof(bmhdrsize), 1, ofile);
  95.         bmhdr.x = bmhdr.y = 0;
  96.         bmhdr.w = win->Width;
  97.         bmhdr.h = win->Height;
  98.         bmhdr.nPlanes = scrn->Depth;
  99.         bmhdr.masking = mskNone;
  100.         bmhdr.compression = packflag ?cmpByteRun1: cmpNone;
  101.         bmhdr.pad1 = 0;
  102.  
  103.         wx = (scrn->Width == 320)? 0: 1;
  104.         wy = (scrn->Height == 200)? 0: 1;
  105.         bmhdr.xAspect = xaspect[wx][wy];
  106.         bmhdr.yAspect = yaspect[wx][wy];
  107.         bmhdr.pageHeight = win->Height;
  108.         bmhdr.pageWidth = scrn->Width;
  109.         bmhdr.transparentColor = 0;
  110.         fwrite((char *)&bmhdr, bmhdrsize, 1, ofile );
  111.     }
  112.  
  113.     fwrite("CMAP",IDSIZE, 1, ofile);
  114.     vp = &win->Screen->ViewPort;
  115.     {
  116.         long cmapsize;
  117.         long i;
  118.         UWORD value;
  119.         UBYTE col[3];
  120.         int numentries;
  121.  
  122.         numentries = (1<< scrn->Depth );
  123.         cmapsize = numentries*3;
  124.         fwrite( (char *)&cmapsize, sizeof(cmapsize), 1,ofile);
  125.         for( i = 0; i < numentries; i++ ) {
  126.               value = GetRGB4(vp->ColorMap, i);
  127.               col[2] = (value & 0xf) << 4;        /* blue */
  128.               col[1] = value & 0xf0; /* green */
  129.               col[0] = (value & 0xf00) >> 4; /* red */
  130.               fwrite(col, 3, 1, ofile);
  131.         }
  132.     }
  133.  
  134.     fwrite("CAMG", IDSIZE, 1, ofile);
  135.     {
  136.         long viewmode;
  137.         long viewmodesize;
  138.  
  139.         viewmodesize = sizeof(viewmode);
  140.         viewmode = scrn->ViewModes;
  141.         fwrite((char *)&viewmodesize, sizeof(viewmodesize), 1, ofile);
  142.         fwrite((char *)&viewmode, sizeof(viewmode), 1, ofile );
  143.     }
  144.  
  145.     fwrite("BODY", IDSIZE,1, ofile);
  146.     {
  147.         struct BitMap *bm;
  148.         long bodypos,
  149.              bodysize;
  150.         UBYTE *bmd[16]; /* assume as many as 16 bit planes :-) */
  151.         int row;
  152.         int plane;
  153.         int rowlength; /* in bytes */
  154.         char outbuff[200]; /* largest enough for a row 1600 bits wide */
  155.  
  156.         bodypos = ftell(ofile);
  157.         fwrite( (char *)&bodysize, sizeof(bodysize), 1, ofile);
  158.         bm = vp->RasInfo->BitMap;
  159.         rowlength = RowBytes(scrn->Width);
  160.  
  161.         for( plane = 0; plane < scrn->Depth; plane++ ) {
  162.             bmd[plane] = bm->Planes[plane] + rowlength * win->TopEdge;
  163.         }
  164.  
  165.         /*
  166.          * write actual bitplanes
  167.          */
  168.         for( row = 0; row < win->Height; row++ ) {
  169.  
  170.             for( plane = 0; plane < scrn->Depth; plane++ ) {
  171.                 if( packflag ) {
  172.                     int packedsize;
  173.  
  174.                     packedsize = PackRow(bmd[plane],outbuff, rowlength );
  175.                     fwrite(outbuff, packedsize, 1, ofile);
  176.                 }
  177.                 else {
  178.                     fwrite(bmd[plane], rowlength, 1, ofile );
  179.                 }
  180.                 bmd[plane] += rowlength;
  181.             }
  182.         }
  183.         bodysize = ftell(ofile) -( bodypos + 4);
  184.         /*
  185.          * fill out body to make even
  186.          */
  187.         if( bodysize & 1 ) {
  188.             fputc(0,ofile);
  189.             bodysize++;
  190.         }
  191.         formsize = ftell(ofile) -( formpos + 4);
  192.  
  193.         fseek( ofile, bodypos, 0L );
  194.         fwrite( (char *)&bodysize, sizeof(bodysize), 1, ofile);
  195.     }
  196.  
  197.     fseek( ofile, formpos, 0L );
  198.     fwrite( (char *)&formsize, sizeof(formsize), 1, ofile);
  199.  
  200.     fclose(ofile);
  201. }
  202.